package com.jiangjiang.eshop.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderStatus;
import com.jiangjiang.eshop.wms.domain.PurchaseInputOrderEntity;
import com.jiangjiang.eshop.wms.dto.*;
import com.jiangjiang.eshop.wms.service.PurchaseInputOrderPutOnItemService;
import com.jiangjiang.eshop.wms.service.PurchaseInputOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PurchaseInputOrderController
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/13 22:52
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/wms/purchaseInputOrder")
public class PurchaseInputOrderController {
    @Autowired
    private PurchaseInputOrderService purchaseInputOrderService;
    @Autowired
    private PurchaseInputOrderPutOnItemService purchaseInputOrderPutOnItemService;

    /**
     * 新增采购入库单
     *
     * @param dto 入参
     * @return 处理结果
     */
    @PostMapping("/save")
    public Boolean save(@RequestBody PurchaseInputOrderDTO dto) {
        try {
            return purchaseInputOrderService.save(dto);
        } catch (Exception e) {
            log.warn("PurchaseInputOrderController save error", e);
        }
        return false;
    }

    /**
     * 更新采购入库单
     *
     * @param dto 入参
     * @return 处理结果
     */
    @PostMapping("/update")
    public Boolean update(@RequestBody PurchaseInputOrderDTO dto) {
        try {
            return purchaseInputOrderService.update(dto);
        } catch (Exception e) {
            log.warn("PurchaseInputOrderController update error", e);
        }
        return false;
    }


    /**
     * 分页查询采购入库单
     *
     * @param query 查询条件
     * @return 分页数据
     */
    @PostMapping("/queryForPage")
    public Page<PurchaseInputOrderDTO> queryForPage(@RequestBody PurchaseInputOrderQuery query) {
        try {
            return purchaseInputOrderService.queryForPage(query);
        } catch (Exception e) {
            log.info("PurchaseInputOrderController queryForPage error", e);
        }
        return null;
    }


    /**
     * 根据采购入单id查询采购入库单数据
     *
     * @param id 采购入库单id
     * @return 查询结果
     */
    @GetMapping("/queryById/{id}")
    public PurchaseInputOrderDTO queryById(@PathVariable("id") Long id) {
        try {
            return purchaseInputOrderService.queryById(id);
        } catch (Exception e) {
            log.warn("PurchaseOrderInputController queryById error", e);
        }
        return null;
    }

    /**
     * 批量保存采购入库单上架明细数据
     *
     * @param dto 采购入库单上架明细数据
     * @return 处理结果
     */
    @PostMapping("/batchSavePutOnItems")
    public Boolean batchSavePutOnItems(@RequestBody PutOnItemsSaveDTO dto) {
        try {
            return purchaseInputOrderPutOnItemService.batchSavePutOnItems(dto.getPutOnItems());
        } catch (Exception e) {

            log.warn("PurchaseInputOrderController batchSavePutOnItems error", e);
        }
        return false;
    }

    /**
     * 采购入库单提交审核
     * @param id 采购入库单id
     * @return 处理结果
     */
    @PostMapping("/submitApprove/{id}")
    public Boolean submitApprove(@PathVariable("id") Long id){
        try {
            return purchaseInputOrderService.submitApprove(id);
        }catch (Exception e){
            log.warn("PurchaseInputOrderController submitApprove error ",e);
        }
        return false;
    }

    /**
     * 审核采购入库单
     * @param dto 采购入库单参数
     * @return 处理结果
     */
    @PostMapping("/approve")
    public Boolean approve(@RequestBody ApprovePurchaseInputDTO dto){
        try {
            return purchaseInputOrderService.approve(dto);
        }catch (Exception e){
            log.warn("PurchaseInputOrderController approve error ",e);
        }
        return false;
    }
}
